package server;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

//SSL 隧道类，用来支持 HTTPS 连接
public class SSLTunnel {

	public SSLTunnel(Socket client, Socket webConnection) throws Exception {
		new Thread(new pointToPoint(client,webConnection)).start();
		new Thread(new pointToPoint(webConnection,client)).start();
	}
	
	
	//将客户端的数据传输到Web中
	class pointToPoint implements Runnable{

		private byte[] temp=new byte[4096];
		private InputStream input;
		private OutputStream output;
		
		private Socket in;
		private Socket out;
		
		public pointToPoint(Socket in,Socket out) throws Exception{
			input=in.getInputStream();
			output=out.getOutputStream();
		}
		
		@Override
		public void run() {
			try{				
				while(true){
					int length=input.read(temp, 0, 4096);
					if(length>0){
						output.write(temp, 0, length);
					}else{
						break;
					}
				}
			}catch(Exception e){
				e.printStackTrace();
			}
			try{
				in.close();
				out.close();
			}catch(Exception e){}
		}
	}
}
